<?php
namespace Home\Controller;

use Think\Controller;

class ConsumpController extends CommonController
{

    public function index(){
    	$Consume = M("consume"); // 消费查询
    	if($_POST['content']!=""){
    		if($_POST['type']==1){
    			$data['client_name'] = array('LIKE','%'.$_POST['content'].'%');
    		}
    		if($_POST['type']==2){
    			$signname = M('user')->where(array('user_name'=>array('like','%'.$_POST['content'].'%')))->find();//签单人姓名
    			$data['referrer_id'] = $signname['user_id'];
    		}
    		if($_POST['type']==3){
    			$data['house_addresss'] = array('LIKE','%'.$_POST['content'].'%');
    		}
    	}
    	if($_GET['p']==''){
    	    $p=1;
    	}
    	else{
    	    $p = $_GET['p'];
    	}
    	$data['isdel']='0';
    	$this->assign('p',$p);
    	$count = $Consume->where($data)->count();
    	$n=15;
    	$page=new \Think\Page($count,$n);
        $show= $page->show();// 分页显示输出
        $this->assign('page',$show);
    	$consume=$Consume->where($data)->page($p.','.$n)->order('consume_id desc')->select();
    	$this->assign('num',$count);
    	$this->assign('consume',$consume);
    	$this->assign('user_auth',session('USER_AUTH'));
        $this->display('Consump/index');
    }
    public function consumptionadd(){
    	$banks = M('bank') ->where('isdel=0 and pid=0')-> select();//银行列表
        $this->assign('banks' , $banks);
        $childbanks = M('bank') ->where('isdel=0 and pid!=0')-> select();//银行列表
          $this->assign('childbanks' , $childbanks);
        $users = M('user')->where(array('isdel'=>'0'))->select();
        $this->assign('users',$users);
        $this->display('Consump/consumption-add');
    }
    public function creatconsumption(){
        $point_id = D('Point') -> addPoint('Consump/creatconsumption', '添加消费档案');//功能节点id
        $roleid = session('USER_AUTH.roleid');//权限角色id
        if( haveAblitity($point_id , $roleid) ){
            if(I('post.client_name') && I('post.loan_amount') && I('post.bank_id') && I('post.referrer_id')){
                $Consume = M("consume"); // 消费表添加
                $data['client_name'] =  I('post.client_name');
                $data['card'] = I('post.card') ;
                $data['creat_time'] = strtotime(I('post.create_time')) ;
                $data['phone'] = I('post.phone') ;
                $data['tel'] = I('post.tel') ;
                $data['work'] = I('post.work') ;
                $data['work_tel'] = I('post.work_tel') ;
                $data['loan_amount'] = I('post.loan_amount') ;
                $data['loan_type'] = I('post.loan_type') ;
                $data['loan_year'] = I('post.loan_year') ;
                $data['bank_id'] = I('post.bank_id') ;
                $data['part_bank_id'] = I('post.part_bank_id') ;
                $user =  D('User')->findUser(array('user_name'=>I('post.referrer_id')));
                if($user){
                    $data['referrer_id'] = $user['user_id'];
                }
                $data['remark'] = I('post.remark') ;
                $data['house_addresss'] = I('post.house_addresss') ;
                $data['mortgage_time'] = strtotime(I('post.mortgage_time')) ;
                $data['credit_time'] = strtotime(I('post.credit_time')) ;
                $data['put_userid'] = $_SESSION['userid'] ;
                $data['send_time'] = empty(I('post.send_time'))?'':strtotime(I('post.send_time'));
                $data['finish_time'] = empty(I('post.finish_time'))?'':strtotime(I('post.finish_time'));
                $consume=$Consume->add($data);
                if($consume){
                    $this -> jsonReply('200' , '添加成功');
                }
                else{
                    $this -> jsonReply('500' , '添加失败');
                }
            }
            else{
                $this -> jsonReply('500' , '请填写客户姓名、贷款金额以及贷款银行');
            }
        }
        else{
            $this -> jsonReply('500' , '您没有权限操作此功能');
        }
    }



    public function consumptionalert(){
    	$consume_id= $_GET['consume_id'];
    	$banks = M('bank') ->where('isdel=0 and pid=0')-> select();//银行列表
        $this->assign('banks' , $banks);
    	$Consume = M("consume"); // 消费表
        $consumelist = $Consume->where('consume_id='.$consume_id)->find();
        if($consumelist['referrer_id']){
            $signname = M('user')->where('user_id='.$consumelist['referrer_id'])->find();//推荐人姓名
            $this->assign('signname' , $signname);
        }
        $this->assign('consumelist',$consumelist);
        $users = M('user')->where(array('isdel'=>'0'))->select();
        $this->assign('users',$users);
        $this->assign('user_auth' , session('USER_AUTH'));
        $this->display('Consump/consumption-alert');
    }

    public function alertconsumption(){
        $point_id = D('Point') -> addPoint('Consump/alertconsumption', '修改消费档案');//功能节点id
        $roleid = session('USER_AUTH.roleid');//权限角色id
        $point = D('Point') -> addPoint('Index/charge', '收费');
        $consume_id= I('post.consume_id');
        $Consume = M("consume"); // 消费表添加
        if( haveAblitity($point_id , $roleid) ){
            $data['client_name'] =  I('post.client_name');
            $data['card'] = I('post.card') ;
            $data['creat_time'] = strtotime(I('post.create_time')) ;
            $data['phone'] = I('post.phone') ;
            $data['tel'] = I('post.tel') ;
            $data['work'] = I('post.work') ;
            $data['work_tel'] = I('post.work_tel') ;
            $data['loan_amount'] = I('post.loan_amount') ;
            $data['loan_type'] = I('post.loan_type') ;
            $data['loan_year'] = I('post.loan_year') ;
            $data['bank_id'] = I('post.bank_id') ;
            $data['send_time'] = empty(I('post.send_time'))?'':strtotime(I('post.send_time'));
            $data['finish_time'] = empty(I('post.finish_time'))?'':strtotime(I('post.finish_time'));
            $user =  D('User')->findUser(array('user_name'=>I('post.referrer_id')));
            if($user){
                $data['referrer_id'] = $user['user_id'];
            }
            $data['remark'] = I('post.remark') ;
            $data['house_addresss'] = I('post.house_addresss') ;
            $data['mortgage_time'] = strtotime(I('post.mortgage_time')) ;
            $data['credit_time'] = strtotime(I('post.credit_time')) ;
            $data['put_userid'] = $_SESSION['userid'] ;
            $consume=$Consume->where('consume_id='.$consume_id)->save($data);
            if($roleid!='4'){
                if($consume){
                $this -> jsonReply('200' , '保存成功');
              }
              else{
                $this -> jsonReply('500' , '保存失败');
              }
            }
        }
        if( haveAblitity('34' , $roleid) ){
            $data['charge_time'] = strtotime(I('post.charge_time')) ;
            $consume=$Consume->where('consume_id='.$consume_id)->save($data);
            if($consume){
                $this -> jsonReply('200' , '保存成功');
              }
              else{
                $this -> jsonReply('500' , '保存失败sdfvwr');
              }
        }
    	else{
            $this -> jsonReply('500' , '您没有权限操作此功能');
        }
    }


    public function delDatas(){
        if(IS_POST){
          $point_id = D('Point') -> addPoint('Consume/delDatas', '批量删除消费档案');//功能节点id
          $roleid = session('USER_AUTH.roleid');//权限角色id
          if( haveAblitity($point_id , $roleid) ){
            $id = I('post.id');
            if(count($id)==1){
              $del = D('Consume') -> saveDoc($id[0],array('isdel'=>'1'));
            }
            if(count($id)>1){
              foreach($id as $k=>$v){
                $del = D('Consume') -> saveDoc($v,array('isdel'=>'1'));
              }
            }
            if($del){
              $this -> jsonReply('200' , '删除成功');
            }
            else{
              $this -> jsonReply('500' , '删除失败');
            }
          }
          else{
            $this -> jsonReply('500' , '您没有权限操作此功能');
          }
        }
      }








}
